package com.hsxshop.controller;

import com.hsxshop.model.ManagerUser;
import com.hsxshop.model.Pagination;
import com.hsxshop.service.ManagerUserService;
import com.hsxshop.util.*;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by wangchongya on 17/7/20.
 */
@Controller
@RequestMapping(value = "/managerUser/")
public class ManagerUserController {
    @Resource(name = "managerUserService")
    private ManagerUserService managerUserService;

    @RequestMapping(value = "list", method = RequestMethod.POST,produces= MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String index(@RequestBody Map requestMap, HttpSession session){
        Pagination pagination= PaginationTool.getPagination(requestMap);
        ManagerUser user =(ManagerUser) session.getAttribute("userToken");
        if(user.getUsername().equals("admin")){
            List<ManagerUser> lists=managerUserService.findByHQLWithPagination(pagination,SqlTool.returnSql("new ManagerUser(id,username,phone,name,state)","ManagerUser"," where username!='admin'","order by id desc"),null);
            return JsonTool.returnListJsonSuccess(lists,pagination);
        }else{
            return JsonTool.returnObjectJsonErrorMsg("无权限访问");
        }
    }
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String add(@RequestBody ManagerUser managerUser) {

        if("".equals(managerUser.getUsername().trim())){
            return JsonTool.returnObjectJsonErrorMsg("账号不能为空");
        }
        if("".equals(managerUser.getName().trim())){
            return JsonTool.returnObjectJsonErrorMsg("名称不能为空");
        }

        if(!ValidateTool.isPhone(managerUser.getPhone())){
            return JsonTool.returnObjectJsonErrorMsg("非法的手机号码");
        }

        if("".equals(managerUser.getPassword().trim())){
            return JsonTool.returnObjectJsonErrorMsg("密码不能为空");
        }
        if(managerUser.getPassword().trim().length()<6){
            return JsonTool.returnObjectJsonErrorMsg("密码长度不能小于6");
        }

        //查找该手机是否已存在
        if(managerUserService.exitsByHQL("from ManagerUser where phone=?0",managerUser.getPhone())){
            return JsonTool.returnObjectJsonErrorMsg("该手机号码已存在");
        }
        managerUser.setPassword(MD5Util.getMD5Str(managerUser.getPassword()));
        managerUser.setState(1);
        if(managerUserService.saveEntity(managerUser)>0){
            return JsonTool.returnObjectJsonSuccess(null);
        }

        return JsonTool.returnObjectJsonErrorMsg("添加失败");
    }

    @RequestMapping(value = "detail", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String detail(@RequestBody Map requestMap){

        Integer id=(Integer)requestMap.get("id");

        ManagerUser managerUser=managerUserService.getEntity(id);

        return JsonTool.returnObjectJsonSuccess(managerUser);
    }
    @RequestMapping(value = "edit", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes =MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String edit(@RequestBody ManagerUser managerUser){

        if("".equals(managerUser.getName().trim())){
            return JsonTool.returnObjectJsonErrorMsg("名称不能为空");
        }

        if(!ValidateTool.isPhone(managerUser.getPhone())){
            return JsonTool.returnObjectJsonErrorMsg("非法的手机号码");
        }

        ManagerUser originalManagerUser=managerUserService.getEntity(managerUser.getId());
        if(originalManagerUser.getPhone().equals(managerUser.getPhone()) && originalManagerUser.getName().equals(managerUser.getName())){
            if((!"".equals(managerUser.getPassword().trim())) && MD5Util.getMD5Str(managerUser.getPassword()).equals(originalManagerUser.getPassword())){
                return JsonTool.returnObjectJsonErrorMsg("未做任何修改");
            }
        }

        if(!originalManagerUser.getPhone().equals(managerUser.getPhone())){
            //查找该手机是否已存在
            if(managerUserService.exitsByHQL("from ManagerUser where phone=?0",managerUser.getPhone())){
                return JsonTool.returnObjectJsonErrorMsg("该手机号码已存在");
            }
            originalManagerUser.setPhone(managerUser.getPhone());
        }
        if(!originalManagerUser.getName().equals(managerUser.getName())){
            originalManagerUser.setName(managerUser.getName());
        }

        if(originalManagerUser.getState()!=managerUser.getState()){
            originalManagerUser.setState(managerUser.getState());
        }
        if((!"".equals(managerUser.getPassword().trim())) && (!MD5Util.getMD5Str(managerUser.getPassword()).equals(originalManagerUser.getPassword()))){
            originalManagerUser.setPassword(MD5Util.getMD5Str(managerUser.getPassword()));
        }

        managerUserService.saveOrUpdateEntity(originalManagerUser);

        return JsonTool.returnObjectJsonSuccess(null);
    }

}
